﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TvTipper.Entities;
using TvTipper.Core;

namespace TvTipper.Controls
{
    public partial class EntityInfoControl : UserControl
    {
        private IDictionary<Type, BaseInfoPanel> m_panels = new Dictionary<Type, BaseInfoPanel>();
        private Entity m_entity;

        public EntityInfoControl()
        {
            InitializeComponent();
            m_rankCtrl.RankControl.ValueChanged += OnRankChanged;
        }

        public void SetInfoPanel(Type entityType, BaseInfoPanel infoPanel)
        {
            m_panels[entityType] = infoPanel;
            var newPage = new TabPage(entityType.ToString());
            newPage.Controls.Add(infoPanel);
            infoPanel.Dock = DockStyle.Fill;
            m_rootControl.TabPages.Add(newPage);
        }

        public void SetEntity(Entity entity)
        {
            if (entity == m_entity)
            {
                return;
            }

            m_entity = entity;
            if (entity == null)
            {
                //none
                m_rootControl.SelectedIndex = 0;
            }
            else
            {
                // can spare here if the same entity type.

                BaseInfoPanel panel;
                if (!m_panels.TryGetValue(entity.GetType(), out panel)){
                    throw new ArgumentException("Unsupported entity type.");
                }

                for (int i = 1; i < m_rootControl.TabPages.Count; i++)
                {
                    if (m_rootControl.TabPages[i].Text.Equals(entity.GetType().ToString()))
                    {
                        m_rootControl.SelectedIndex = i;
                        break;
                    }
                }

                panel.LoadEntity(entity);
            }

            var rankable = entity as RankableEntity;
            if (rankable != null)
            {
                m_rankCtrl.SetEntity(rankable);

                m_wiki.Links.Clear();
                m_website.Links.Clear();

                if (!string.IsNullOrEmpty(rankable.WikiUrl))
                {
                    var id  = new Uri(rankable.WikiUrl).Segments.Last();
                    var uri = string.Format("http://en.wikipedia.org/wiki/index.html?curid={0}", id);
                    m_wiki.Links.Add(0, m_wiki.Text.Length, uri);
                    m_wiki.Enabled = true;
                }
                else
                {
                    m_wiki.Enabled = false;
                }


                if (!string.IsNullOrEmpty(rankable.Website))
                {
                    m_website.Links.Add(0, m_website.Text.Length, rankable.Website);
                    m_website.Enabled = true;
                }
                else
                {
                    m_website.Enabled = false;
                }
            }
        }

        private void OnLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
        }

        private void OnRankChanged(object sender, TvTipper.Controls.RankControl.RankEventArgs e)
        {
            var rankable = (RankableEntity)m_entity;
            rankable.UserRank = e.NewValue;
            Services.DataAccessPortal.RankEntity(rankable, m_rankCtrl.RankControl.Value);
        }

        private void OnEditClicked(object sender, EventArgs e)
        {
            if (m_entity is Person)
            {
                var person = (Person)m_entity;

                var form = new PersonEditForm()
                {
                    PersonName = person.Name,
                    Description = person.Info
                };
                if (form.ShowDialog() == DialogResult.OK)
                {
                    person.Name = form.PersonName;
                    person.Info = form.Description;
                    Services.DataAccessPortal.UpdatePerson(person);
                }
            }
            else
                if (m_entity is TvProgram)
                {
                    var program = (TvProgram)m_entity;

                    var form = new ProgramEditForm()
                    {
                        ProgramName = program.Name,
                        Description = program.Description,
                        StartYear = program.StartYear,
                        EndYear = program.EndYear,
                        Seasons = program.Seasons
                    };

                    if (form.ShowDialog() == DialogResult.OK)
                    {
                        program.Name = form.ProgramName;
                        program.Description = form.Description;
                        program.Seasons = form.Seasons;
                        program.StartYear = form.StartYear;
                        program.EndYear = form.EndYear;

                        Services.DataAccessPortal.UpdateProgram(program);
                    }
                }
        }
    }
}
